import json

from Basic.global_variables_path import *

def pytest_terminal_summary(terminalreporter, exitstatus, config):
    skipped = terminalreporter.stats.get("skipped", [])
    passed = terminalreporter.stats.get("passed", [])
    failed = terminalreporter.stats.get("failed", [])
    total_num = terminalreporter._numcollected
    date_time = time.strftime("%Y-%m-%d %H.%M.%S")
    RESULT_PATH = os.path.join(global_project_path, f"results\\{date_time}--result.json")
    result = {}
    if skipped:
        for i in skipped:
            result.update({
                i.nodeid: {
                    "result": "skiped",
                    "date_time": date_time
                }
            })
    if passed:
        for i in passed:
            result.update({
                i.nodeid: {
                    "result": "passed",
                    "date_time": date_time
                }
            })
    if failed:
        for i in failed:
            result.update({
                i.nodeid: {
                    "result": "failed",
                    "date_time": date_time
                }
            })
    open(RESULT_PATH, "w").write(json.dumps(result))
    # print("用例总数:", total_num)
    # print("跳过的用例数为：", len(skipped))
    # print("通过的用例数为：", len(passed))
    # print("失败的用例数为：", len(failed))
    #
    # print("跳过的用例们：", skipped)
    # print("通过的用例们：", passed)
    # print("失败的用例们：", failed)

    # failed_name = []
    # for i in failed:
    #     # #获取所有数据
    #     # print(dir(i))
    #     # for j in i.__dict__:
    #     #     i_data = getattr(i, j)
    #     #     print(f'i里面的数据{j}', i_data)
    #     #     print(f'i里面的数据{j}', i.longrepr)
    #     print('失败的用例名称：', i.nodeid)
    #     print('失败的日志：-----------------------------------------------------\n', i.longrepr)
    #     print('-------------------------------------------------------------', )
    #     failed_name.append(i.nodeid)
    #     case_name = i.nodeid.replace('/', '.').replace('.py::', '.').replace('::', '.')
    #
    #     log_path = os.path.join(global_project_path, f"results\\{date} {case_name}.log")
    #     print(log_path)
    #     with open(log_path, 'w', encoding='UTF-8') as f:
    #         f.write(str(i.longrepr))
    # print("失败的用例为", failed_name)

def pytest_itemcollected(item):
    # team = {}
    # print("用例标签", item.nodeid)
    # print("用例标签", item.own_markers)
    # for i in item.own_markers:
        # print(item.nodeid)
        # print(i.__dict__)
    if [i for i in item.own_markers if i.name == 'item']:
        for i in [i for i in item.own_markers if i.args]:
            print('用例标签：', i.args[0])
